<?php

namespace Admin\Util;

	class ExcelUtil{
		static private $cellName = array('A','B','C','D','E','F','G','H','I','J',
	        	'K','L','M','N','O','P','Q','R','S','T','U','V','W','X',
	        	'Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ',
	        	'AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV',
	        	'AW','AX','AY','AZ','BA','BB','BC','BD','BE','BF','BG','BH',
	        	'BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT',
	        	'BU','BV','BW','BX','BY','BZ'); 

		/**
		 * [__construct]
		 * @Author   :seven
		 * @DateTime :2016-12-01T16:42:51+0800
		 * @Detail   :
		 */
		private function ExcelControl($expTitle,$expCellName,$expTableData){
			/*设置字符集编码*/
	        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);
	        $fileName = $_SESSION['account'].date('_YmdHis');

	        $cellNum = count($expCellName);
	        $dataNum = count($expTableData);

	        /*生成操作对象*/
	        vendor("PHPExcel.PHPExcel");
	        $objPHPExcel = new \PHPExcel();
	        
	        /*设置标题*/
	        for($i=0;$i<$cellNum;$i++){
	            $objPHPExcel
	            ->setActiveSheetIndex(0)
	            ->setCellValue(self::$cellName[$i].'1', $expCellName[$i][1]); 
	        } 
	        /*布置数据*/
	        for($i=0;$i<$dataNum;$i++){
				for($j=0;$j<$cellNum;$j++){
					$objPHPExcel
						->getActiveSheet(0)
						->setCellValue(self::$cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
				}             
	        }  
	        
	        header('pragma:public');
	        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
	        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
	        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
	        $objWriter->save('php://output'); 
	        exit;
    	}
		/**
	     * 导出Excel
	     */
	    public function exportExcel($xlsData,$cellModel = NULL){
	        $xlsName  = "Order Export Excel";
	        if(is_null($cellModel)){
	        	foreach ($xlsData[0] as $key => $value) {
	        		$xlsCell[] = array($key,$key);
	        	}
	        }else{
	        	$index = 0;
	        	foreach ($xlsData[0] as $key => $value) {
	        		$xlsCell[] = array($key,$cellModel[$index++]);
	        	}
	        }

	        $this->ExcelControl($xlsName,$xlsCell,$xlsData);
	    }


	    static public function importExecl($file, $extension){
	    	if(!file_exists($file)){ 
	            return array("error"=>0,'message'=>'file not found!');
	        } 
	        Vendor("PHPExcel.PHPExcel.IOFactory");

	        if($extension === 'xlsx'){
	        	$objReader = \PHPExcel_IOFactory::createReader('Excel2007'); 
	        }else{
	        	$objReader = \PHPExcel_IOFactory::createReader('Excel5');
	        }
	        

	        try{
	            $PHPReader = $objReader->load($file);
	        }catch(Exception $e){
	        	exit('Read Excel File Error');
	        }

	        $currentSheet = $PHPReader->getSheet(0);
	        /**取得最大的列号*/  
	        $allColumn = $currentSheet->getHighestColumn();  
	        /**取得一共有多少行*/  
	        $allRow = $currentSheet->getHighestRow();
	        $res = array();
	        for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){  
	            for($colIndex='A';$colIndex<=$allColumn;$colIndex++){  
	                $addr = $colIndex.$rowIndex; 

	                $cell = $currentSheet->getCell($addr)->getValue();
	                //富文本转换字符串 
	                if($cell instanceof \PHPExcel_RichText)       
	                    $cell = $cell->__toString();  

	                $arr[] = $cell;
	            }
	            $res[] = $arr;
	            unset($arr);
	        }
	        
	        return $res;
	    }
	}
